package com.aboo.dao.impl;

import java.util.List;

import com.aboo.dao.AdminUserDao;
import com.aboo.dao.BaseSqlMapDao;
import com.aboo.po.AdminUserPO;
import com.aboo.po.MenuPO;
import com.aboo.po.Page;
import com.aboo.po.RolePO;
import com.aboo.pub.constant.Constant;
import com.aboo.util.EncryptionUtil;

public class AdminUserDaoImpl extends BaseSqlMapDao implements AdminUserDao {

	public Integer save(AdminUserPO po) throws Exception {
		po.setPassword(EncryptionUtil.encodeMD5(po.getPassword()));
		return (Integer) getSmcTemplate().insert("AdminUser.insertAdminUser", po);
	}

	public void delete(Integer id) throws Exception {
		AdminUserPO tmp = new AdminUserPO();
		tmp.setId(id);
		tmp.setInUse(Constant.INUSE_NO);
		getSmcTemplate().update("AdminUser.updateAdminUser", tmp);
	}

	public AdminUserPO find(AdminUserPO po) throws Exception {
		return (AdminUserPO) getSmcTemplate().queryForObject("AdminUser.selectAdminUserList", po);
	}

	public Integer findCount(AdminUserPO po) throws Exception {
		return (Integer) getSmcTemplate().queryForObject("AdminUser.selectAdminUserCount", po);
	}

	public List<AdminUserPO> findList(AdminUserPO po, Page page)
			throws Exception {
		if (page != null) {
			po.setBeginIndex(page.getBeginRecordIndex());
			po.setEndIndex(page.getEndRecordIndex());
		}		
		po.addSort("ID", Constant.SORT_ASC);
		return getSmcTemplate().queryForList("AdminUser.selectAdminUserList", po);
	}

	public void update(AdminUserPO po) throws Exception {
		getSmcTemplate().update("AdminUser.updateAdminUser", po);
	}

	public List<MenuPO> findMenuById(Integer id) throws Exception {
		return getSmcTemplate().queryForList("AdminUser.selectMenuById", id);
	}
	
	public List<RolePO> findRoleById(Integer id) throws Exception {
		return getSmcTemplate().queryForList("AdminUser.selectRoleById", id);
	}
	
	public AdminUserPO validateAdminUser(AdminUserPO param) throws Exception {
		return (AdminUserPO) getSmcTemplate().queryForObject("AdminUser.validateAdminUser", param);
	}

}
